var gp = require('gulp')
var concat = require('gulp-concat')
var rename = require('gulp-rename')
var uglify = require('gulp-uglify')
var replace = require('gulp-replace')

function buildUMD (options, config) {
  return gp.src(['./template/umd-start.text'].concat(options.paths).concat(['./template/umd-end.text']))
    .pipe(replace('@@@NAME', config.name))
    .pipe(replace('@@@EXPORT', options.export))
    .pipe(replace('@@@VERSION', config.version))
    .pipe(concat(config.name + '.js'))
    .pipe(replace(/(\n'use strict')/g, ''))
    .pipe(replace(/(\/\*\s{1}CommonJS\s{1}\*\/[^\n]+)/g, ''))
    .pipe(replace(/(\nvar\s{1}\{\s{1}[\s,_a-zA-Z0-9]+\s{1}\}\s{1}=\s{1}require\(.+\)[^\n]+)/g, ''))
    .pipe(replace(/(\nvar\s{1}[_a-zA-Z0-9]+\s{1}=\s{1}require\(.+\)[^\n]+)/g, ''))
    .pipe(replace(/(\nmodule.exports\s{1}={1}[^\n]+)/g, ''))
    .pipe(replace(/(\nmodule.exports\.default\s{1}={1}[^\n]+)/g, ''))
    .pipe(gp.dest('../' + config.name + '/dist'))
    .pipe(replace('\'development\'', '\'production\''))
    .pipe(uglify({
      output: {
        comments: 'some'
      }
    }))
    .pipe(rename({
      extname: '.min.js'
    }))
    .pipe(gp.dest('../' + config.name + '/dist'))
}

gp.task('vxe-ajax', function () {
  return buildUMD({
    export: 'VXEAjax',
    paths: [
      '../vxe-ajax/index.js'
    ]
  }, require('../vxe-ajax/package.json'))
})

gp.task('xe-ajax', function () {
  return buildUMD({
    export: 'XEAjax',
    paths: [
      '../xe-ajax/src/core/utils.js',
      '../xe-ajax/src/core/setup.js',
      '../xe-ajax/src/handle/progress.js',
      '../xe-ajax/src/handle/headers.js',
      '../xe-ajax/src/handle/readableStream.js',
      '../xe-ajax/src/handle/abortSignal.js',
      '../xe-ajax/src/handle/abortController.js',
      '../xe-ajax/src/handle/interceptor.js',
      '../xe-ajax/src/handle/request.js',
      '../xe-ajax/src/handle/response.js',
      '../xe-ajax/src/handle/index.js',
      '../xe-ajax/src/adapters/xhr.js',
      '../xe-ajax/src/adapters/fetch.js',
      '../xe-ajax/src/adapters/jsonp.js',
      '../xe-ajax/src/core/ajax.js',
      '../xe-ajax/src/core/index.js',
      '../xe-ajax/index.js'
    ]
  }, require('../xe-ajax/package.json'))
})

gp.task('xe-ajax-mock', function () {
  return buildUMD({
    export: 'XEAjaxMock',
    paths: [
      '../xe-ajax-mock/src/core/utils.js',
      '../xe-ajax-mock/src/core/setup.js',
      '../xe-ajax-mock/src/core/store.js',
      '../xe-ajax-mock/src/template/mixin.js',
      '../xe-ajax-mock/src/template/bulid.js',
      '../xe-ajax-mock/src/template/index.js',
      '../xe-ajax-mock/src/adapters/xhr.js',
      '../xe-ajax-mock/src/adapters/fetch.js',
      '../xe-ajax-mock/src/adapters/jsonp.js',
      '../xe-ajax-mock/src/handle/response.js',
      '../xe-ajax-mock/src/handle/result.js',
      '../xe-ajax-mock/src/handle/index.js',
      '../xe-ajax-mock/src/core/mock.js',
      '../xe-ajax-mock/src/core/index.js',
      '../xe-ajax-mock/index.js'
    ]
  }, require('../xe-ajax-mock/package.json'))
})

gp.task('vxe-utils', function () {
  return buildUMD({
    export: 'VXEUtils',
    paths: [
      '../vxe-utils/index.js'
    ]
  }, require('../vxe-utils/package.json'))
})

gp.task('xe-utils', function () {
  return buildUMD({
    export: 'XEUtils',
    paths: [
      '../xe-utils/src/core/utils.js',
      '../xe-utils/src/core/setup.js',
      '../xe-utils/src/method/base.js',
      '../xe-utils/src/method/array.js',
      '../xe-utils/src/method/browse.js',
      '../xe-utils/src/method/cookie.js',
      '../xe-utils/src/method/date.js',
      '../xe-utils/src/method/locat.js',
      '../xe-utils/src/method/number.js',
      '../xe-utils/src/method/string.js',
      '../xe-utils/src/method/index.js',
      '../xe-utils/src/core/index.js',
      '../xe-utils/index.js'
    ]
  }, require('../xe-utils/package.json'))
})

gp.task('xe-command', function () {
  return buildUMD({
    export: 'XECommand',
    paths: [
      '../xe-command/index.js'
    ]
  }, require('../xe-command/package.json'))
})

gp.task('xe-clipboard', function () {
  return buildUMD({
    export: 'XEClipboard',
    paths: [
      '../xe-clipboard/index.js'
    ]
  }, require('../xe-clipboard/package.json'))
})
